package com.hands.transfercenter.baiduApi;



import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.json.JSONException;

public class ApiNlpDataHelper {


    /**
     * 分词解析 （action：为空，则返回所有item ， action：不为空，返回对应的分词内容）
     * @param speek
     * @param action
     * @return
     * @throws JSONException
     */
    public  static  String  getWords(String speek,String action) throws JSONException {

        org.json.JSONObject obj =  AipNlpClass.handelString(speek);
        System.out.println("分词信息返回0"+obj);
        JSONArray arr = JSONArray.fromObject(obj.get("items").toString());
        String vret = "";
        JSONArray arrItem = new JSONArray();

            for (int i=0;i<arr.size();i++)
            {
                JSONObject myjObject = JSONObject.fromObject(arr.get(i));
                String pos = myjObject.get("pos").toString();
                if (action.equals(pos)){
                    String  vPosItem = myjObject.get("item").toString();
                    return vPosItem;
                }else
                {
                    String  vPosItem = myjObject.get("item").toString();
                    arrItem.add(vPosItem);
                }
            }
        vret = arrItem.toString();



        return  vret;
    }

    /**
     * 解析语义，获取相关结果
     * @param speek
     * @param action
     * @return
     */
    public  static  String  nlpResult(String speek,String action) throws JSONException {
        org.json.JSONObject obj =  AipNlpClass.handelString(speek);
        System.out.println("分词信息返回0"+obj);
        JSONArray arr = JSONArray.fromObject(obj.get("items").toString());
        String vret = "";
        for (int i=0;i<arr.size();i++)
        {
            JSONObject myjObject = JSONObject.fromObject(arr.get(i));
            String pos = myjObject.get("pos").toString();
            if (action.equals(pos)){
              String  vPosItem = myjObject.get("item").toString();
                if(action.equals("m")){
                    if(vPosItem.indexOf("种")>=0 && vPosItem.indexOf("第")>=0){
                        if(vret.equals(""))
                            vret = vPosItem.substring(1,vPosItem.length()-1);
                    }else if(vPosItem.indexOf("瓶")>=0 || vPosItem.indexOf("件")>=0 || vPosItem.indexOf("包")>=0 || vPosItem.indexOf("箱")>=0){
                        if(!vret.equals("")){
                            vret += ","+vPosItem.substring(0,vPosItem.length()-1);
                        }else
                        {
                            vret += vPosItem.substring(0,vPosItem.length()-1);
                        }
                    }
                }
            }
            //return vret;
            /*else  if ( action.equals("n") || action.equals("vn") || action.equals("nw")) {
                vPosItem = myjObject.get("item").toString();
                return   vPosItem;
            }*/

          /* JSONObject myjObject = JSONObject.fromObject(arr.get(i));
           String pos = myjObject.get("pos").toString();
           if (action.equals(pos) && !action.equals("m")) { //动词
               vPosItem = myjObject.get("item").toString();
               return  vPosItem;
           } else if (action.equals("m")) { //量词
               String[] basicWords = myjObject.get("basic_words").toString().replace("[", "").replace("]", "").split(",");
               *//*for (int j = 0; j < basicWords.length; j++) {
                   int num = TextConvertNumber.chineseNumber2Int(basicWords[j]);
                   System.out.println("转换数字为：" + num);
               }*//*

           }*/
           /*else if (action.equals("n") || action.equals("vn") || action.equals("nw")) { //名词等
               vPosItem = myjObject.get("item").toString();
           }*/
        }
        return  vret;
    }

}
